package org.springblade.modules.client;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.library.AffiliatioType;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.library.entity.QuestionOptionEntity;
import org.springblade.modules.library.service.IQuestionBankService;
import org.springblade.modules.library.service.IQuestionOptionService;
import org.springblade.modules.library.vo.QuestionBankVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 题库练习相关功能接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-client/bank")
@Api(value = "客户端题库练习相关接口" ,tags ="客户端题库练习相关接口" )
public class ClientQuestionBankController {

	private final IQuestionBankService questionBankService;
	private final IQuestionOptionService questionOptionService;
	private final CommonUtil commonUtil;
	/**
	 * 题库习题分页列表
	 */
	@GetMapping("/question-list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "题库习题分页列表", notes = "可传入课程、章节、小节id、题目类型、题目性质、题目难度等")
	public R questionBankList(QuestionBankVO questionBank, Query query) {
		//获取用户的涉密等级
		questionBank.setSecrecyLevel(commonUtil.getUserBaseEntity().getSecrecyLevel());
		questionBank.setAffiliationType(AffiliatioType.PRACTICE.getCode());

		IPage<QuestionBankVO> pages = questionBankService.clientQuestionBankList(Condition.getPage(query), questionBank);

		List<QuestionBankVO> questionBankVOList=pages.getRecords();
		//获取试题id集
		List<Long> questionBankIds= questionBankVOList.stream()
			.map(QuestionBankVO::getId)
			.collect(Collectors.toList());

		if (CollectionUtil.isEmpty(questionBankIds)) return R.data(pages);
		//获取试题集的所有选项  并根据试题id分组
		Map<Long, List<QuestionOptionEntity>> optionMap = questionOptionService.list(Wrappers.<QuestionOptionEntity>query().lambda()
			.in(QuestionOptionEntity::getQuestionBankId, questionBankIds)).stream().collect(Collectors.groupingBy(QuestionOptionEntity::getQuestionBankId));

		//给指定试题赋值选项
		questionBankVOList.forEach(questionBankVO -> {
			List<QuestionOptionEntity> questionOptionEntityList = optionMap.get(questionBankVO.getId());
			if (CollectionUtil.isEmpty(questionOptionEntityList)) {//如果是简答题，选项有可能为空，需要默认给它提供一个选项
				questionOptionEntityList=new ArrayList<>();
				QuestionOptionEntity questionOptionEntity=new QuestionOptionEntity();
				questionOptionEntity.setQuestionBankId(questionBankVO.getId());
				questionOptionEntityList.add(questionOptionEntity);
			}
			questionBankVO.setOptionVOList(questionOptionEntityList);
		});

		return R.data(pages);
	}


}
